<?php
/**
 * @name OrderController
 * @author xuebingwang
 * @desc 商城订单控制器
 * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
*/
class OrderController extends MallController {
    
    protected $uid;

    public function init(){
        
        parent::init();
    }
    
    public function dopayAction($sn='',$code=''){
         
        if(empty($sn)){
            $this->error('订单编号不能为空！');
        }

        if(empty($code)){
            $this->error('支付方式ID不能为空!');
        }

        $model = new Model('t_payment_setting');
        $payment = $model->get('*',['AND'=>['status'=>1,'code'=>$code]]);

        if(empty($payment)){
            $this->error('支付方式不存在 ！');
        }

        $curl = new Curl();
        $order = $curl->setData(['sn'=>$sn])->send('mall/public/getOrder');
        if(empty($order) || $order['errcode'] != 0){
            $this->error('订单不存在!');
        }

        if($order['status'] != OrderModel::STATUS_WATI_PAY || $order['pay_status'] != OrderModel::PAY_STATUS_NO_PAY){
            $this->error('订单状态不正确!');
        }

        $order['desc'] = $order['product_desc'];
        $order['show_url'] = '';

        if($code == 'wxpay'){
            $payment['appid'] = $this->config->wechat->appid;
            $payment['appsecret'] = $this->config->wechat->appsecret;

            $pay_server = new \Payment\Wxpay\Wxpay($payment);
        }else{

            $pay_server = new \Payment\Alipay\Alipay($payment);
        }
        $this->getView()->assign('order',$order);
        $this->getView()->assign('not_wx', is_not_wx());
//        echo '<textarea>';
//        echo $pay_server->setOrder($order)->doPay();
        $this->getView()->assign('form_html',$pay_server->setOrder($order)->doPay());
        $this->getView()->display('order/dopay.php');
//        echo '</textarea>';die;
    }

    public function payAction($sn=''){

        $user = session('user_auth');

        if(empty($user) || !isset($user['unionid'])){
            $this->redirect('/login.html');
        }

        if(empty($sn)){
            $this->error('订单编号不能为空！');
        }

        $curl = new Curl();
        $order = $curl->setData(['sn'=>$sn,'unionid'=>$user['unionid'],'need_p'=>1])->send('mall/order/getDetail');

        if(empty($order) || $order['errcode'] != 0){
            $this->error('订单不存在!');
        }

        if($order['status'] != OrderModel::STATUS_WATI_PAY || $order['pay_status'] != OrderModel::PAY_STATUS_NO_PAY){
            $this->error('订单状态不正确!');
        }

        $this->getView()->assign('backurl',$this->getRequest()->getQuery('backurl','/member/order/index.html'));
        $this->getView()->assign('order',$order);
        if(intval($user['subscribe']) < 1){
            $this->layout->alert_msg = '<p>付款完成后，请选择关注选项</p><p>您将获得店铺大掌柜资格</p><p>可以获得更多销售佣金！</p>';
        }
    }
    
    /**
     * 结算
     */
    public function balanceAction(){

        $user = session('user_auth');

        if(empty($user) || !isset($user['unionid'])){
            $this->redirect('/login.html');
        }

        $model = new CartModel();
        $products   = $model->getCheckedCart();
        if(empty($products)){
            $this->error('购物车是空的！');
        }

        $data = [
                    'unionid'=>$user['unionid'],
                    'add_ip'=>get_client_ip(),
                    'products'=>$products
                ];
        $curl = new Curl();
        $resp = $curl->setData($data)->send('mall/order/generate');
        if(empty($resp) || $resp['errcode'] !== 0){
            $this->error($resp['errmsg']);
        }

        $payment = $this->getRequest()->getPost('payment');
        $data = ['status'=>1,'msg'=>$resp['errmsg'],'order_sn'=>$resp['order_sn']];

        if($payment != 'wxpay'){
            $data['url'] = '/order/dopay/sn/'.$resp['order_sn'].'/code/'.$payment.'.html';
        }
	
        //清空购物车
        $model->clear();

        $this->ajaxReturn($data);
    }

    public function checkPayAction($sn=''){
        if(empty($sn)){
            $this->error('订单号不能为空!');
        }

        $model = new Model('t_orders');
        $where = [
            'sn'=>$sn,
            'pay_status'=>OrderModel::PAY_STATUS_PAYD
        ];
        if($model->get('sn',['AND'=>$where])){
            $this->success('订单已经付款成功!','/member/order/index.html');
        }else{
            if(IS_AJAX){
                $this->error();
            }
            $this->error('订单尚未支付成功，如果您已经支付成功。请联系管理员');
        }
    }
}
